package uhttp

import (
	"bytes"
	"encoding/json"
	"errors"
	"io/ioutil"
	"net/http"
	"net"
	"crypto/tls"
	"strings"
	"time"
)

/**
 http方式的post操作
 */
func HttpPostJSON(url string, v interface{}) (response []byte, err error) {
	var bs []byte
	bs, err = json.Marshal(v)
	if err != nil {
		return
	}

	bf := bytes.NewBuffer(bs)

	var resp *http.Response
	resp, err = http.Post(url, "application/json", bf)
	if err != nil {
		return
	}

	if resp.Body != nil {
		defer resp.Body.Close()
		response, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			return
		}
	}

	if resp.StatusCode != 200 {
		err = errors.New("status code not equals 200")
	}

	return
}


/**
 https方式post操作，不对服务端的证书进行s校验
 */
func HttpsPostJSON(url string, v interface{}) (response []byte, err error) {
	// 将struct转换为buf
	var bs []byte
	bs, err = json.Marshal(v)
	if err != nil {
		return
	}
	bf := bytes.NewBuffer(bs)

	// 创建tls的不校验服务端的证书的client
	tr := http.Transport{
		Proxy: http.ProxyFromEnvironment,
		DialContext: (&net.Dialer{
			Timeout:   30 * time.Second,
			KeepAlive: 30 * time.Second,
			DualStack: true,
		}).DialContext,
		MaxIdleConns:          100,
		IdleConnTimeout:       90 * time.Second,
		TLSHandshakeTimeout:   10 * time.Second,
		ExpectContinueTimeout: 1 * time.Second,
	}
	tr.TLSClientConfig = &tls.Config{ InsecureSkipVerify: true }

	client := &http.Client{
		Transport: &tr,
		Timeout: 60 * time.Second,
	}

	var resp *http.Response
	resp, err = client.Post(url, "application/json", bf)
	if err != nil {
		return
	}

	if resp.Body != nil {
		defer resp.Body.Close()
		response, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			return
		}
	}

	if resp.StatusCode != 200 {
		err = errors.New("status code not equals 200")
	}

	return
}

/**
 http get方式
 */
func HttpGet(uri string) string {
	resp, err := http.Get(uri)
	if err != nil {
		// handle error
		return ""
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		// handle error
		return ""
	}
	return string(body)
}

/**
 https get方式
 */
func HttpsGet(uri string) string {
	// 创建tls的不校验服务端的证书的client
	tr := http.Transport {
		Proxy: http.ProxyFromEnvironment,
		DialContext: (&net.Dialer{
			Timeout:   30 * time.Second,
			KeepAlive: 30 * time.Second,
			DualStack: true,
		}).DialContext,
		MaxIdleConns:          100,
		IdleConnTimeout:       90 * time.Second,
		TLSHandshakeTimeout:   10 * time.Second,
		ExpectContinueTimeout: 1 * time.Second,
	}
	tr.TLSClientConfig = &tls.Config{ InsecureSkipVerify: true }

	client := &http.Client{
		Transport: &tr,
		Timeout: 60 * time.Second,
	}

	resp, err := client.Get(uri)
	if err != nil {
		// handle error
		return ""
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		// handle error
		return ""
	}
	return string(body)
}

/**
 http方式的post操作
 */
func HttpPost(url string, data string) (response []byte, err error) {
	var resp *http.Response
	resp, err = http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data))
	if err != nil {
		return
	}

	if resp.Body != nil {
		defer resp.Body.Close()
		response, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			return
		}
	}

	if resp.StatusCode != 200 {
		err = errors.New("status code not equals 200")
	}

	return
}
